package com.console.operations.loanmgr;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import com.common.test.GetDataFromImportFile;
import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.common.util.GetTempleteFileUtil;
import com.console.pages.loanmgr.LoanBatchPage;

public class OneLineLoanBatch extends OperationBase {

	public OneLineLoanBatch(UITestBase tb){
		super(tb);
	}
	
	//导入的理财计划单标状态是否为审核中
	public boolean isstatuscorrected = true;
	
	public void importfile() throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        String datestr = sdf.format(date);
        Random random = new Random();
        int x = random.nextInt(8999);
        x = x+1000;
        String tempstr = datestr + String.valueOf(x);
        String loantitle = GetDataFromImportFile.getOneLineDataFromCell(GetTempleteFileUtil.GRAB_FOREMAN_LOAN_ONELINE, "借款标题");
        String contractno = GetDataFromImportFile.getOneLineDataFromCell(GetTempleteFileUtil.GRAB_FOREMAN_LOAN_ONELINE, "合同编号");
		if(DBUtil.isQueryData("select *\n"
							+ "from fiz_loan\n"
							+ "where vc_name = ?\n"
							+ "  and dc_type = ?", new Object[]{loantitle, "00"})){
			log.info("在系统内找到债权名称为 " + loantitle + " 的单标债权");
			DBUtil.update("update fiz_loan\n"
						+ "set vc_name = ?\n"
						+ "where vc_name = ?\n"
						+ "  and dc_type = ?", new Object[]{tempstr, loantitle, "00"});
		}
		if(DBUtil.isQueryData("select *\n"
							+ "from fiz_loan_prop\n"
							+ "where vc_code = 'contract-no'\n"
							+ "  and vc_value = ?", new Object[]{contractno})){
			DBUtil.update("update fiz_loan_prop\n"
						+ "set vc_value = ?\n"
						+ "where vc_code = 'contract-no'\n"
						+ "  and vc_value = ?", new Object[]{tempstr, contractno});
		}
		switchToDefaultContent();
		LoanBatchPage lbp = new LoanBatchPage(tb);
		lbp.upLoadFile.chooseFile(GetTempleteFileUtil.GRAB_FOREMAN_LOAN_ONELINE);
		pause(1000);
		lbp.upLoadButton.click();
		waitForElementExist(lbp.importButton);
		//点击导入按钮
		lbp.importButton.click();
		pause(1500);
		String status = DBUtil.queryOneCell("select dc_status\n" +
			    							"from fiz_loan\n" +
			    							"where pk_id = (\n" +
			    										   "select fk_loan_id\n" +
			    										   "from fiz_loan_prop\n" +
			    										   "where vc_code = ?\n" +
			    										     "and vc_value = ?\n" +
			    										  ")\n" +
			    							  "and dc_type = ?", new Object[]{"contract-no", contractno, "00"});
		if(!status.equals("00")){
			log.error("单标合同号 " + contractno + "对应债券状态不正确");
		}
		isstatuscorrected = status.equals("00");
	}
}
